package com.example.no1admin.repo.impl;

import com.example.no1admin.pojo.vo.picture.PictureListItemVO;
import com.example.no1admin.repo.IPictureRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Repository
@Slf4j
public class PictureRepositoryImpl implements IPictureRepository {

    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    @Override
    public List<PictureListItemVO> getList() {
        List<Serializable> list = redisTemplate.opsForList().range(KEY_PICTURE_LIST, 0, -1);
        List<PictureListItemVO> listItemVOS = new ArrayList<>();
        for (Serializable serializable : list) {
            listItemVOS.add((PictureListItemVO) serializable);
        }
        log.debug("从Redis中读取图片列表");
        return listItemVOS;
    }

    @Override
    public void putList(List<PictureListItemVO> list) {
        log.debug("向Redis插入品牌列表数据，{}", list);
        for (PictureListItemVO pictureListItemVO : list) {
            redisTemplate.opsForList().rightPush(KEY_PICTURE_LIST, pictureListItemVO);
        }
    }

    @Override
    public void deleteList() {
        redisTemplate.delete(KEY_PICTURE_LIST);
    }
}
